#include "KDMDetector.hh"
#include "KDMHitsCollection.hh"
#include "KDMHit.hh"
#include "G4SDManager.hh"
#include "KDMGlobals.hh"
#include <sstream>
#include <iostream>
using namespace std;

KDMDetector::KDMDetector(G4String name) :
	G4VSensitiveDetector(name) {
	hitsCollection = 0;
	collectionName.insert("1");
}

KDMDetector::~KDMDetector() {
	;
}

void KDMDetector::Initialize(G4HCofThisEvent* hcc) {
	hitsCollection = new KDMHitsCollection(SensitiveDetectorName, collectionName[0]);
	G4SDManager* sdMan = G4SDManager::GetSDMpointer();
	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID, hitsCollection);
}

void KDMDetector::EndOfEvent(G4HCofThisEvent*) {
	;
}

G4bool KDMDetector::ProcessHits(G4Step *step, G4TouchableHistory*) {
	double deposit = step->GetTotalEnergyDeposit();
	if (deposit == 0.0)
		return false;

	G4Track* track = step->GetTrack();
	double time = track->GetGlobalTime();
	G4ThreeVector pos = track->GetPosition();

	KDMHit* hit = new KDMHit(time, deposit, pos);
	hitsCollection->Insert(hit);

	return true;
}
